package baseAPI;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

/**
 *  右下角弹出式提示框
 *  1.自动上升
 *  2.停留一段时间，本例子中5秒
 *  3.自动下降直至消失
 *  4.线程控制窗口的出现和消失，同时添加鼠标事件控制，可以提前使提示框消失
 */
public class MessageBox extends JWindow implements Runnable, MouseListener {

    private int screenWidth;  // 屏幕宽度
    private int screenHeight; // 屏幕高度
    private int windowWidth = 200; // 设置提示窗口宽度
    private int windowHeight = 150; // 设置提示窗口高度
    private int bottomToolKitHeight; // 底部任务栏高度，如果没有任务栏则为零
    private int stayTime = 7000; // 提示框停留时间
    private int x; // 窗口起始X坐标
    private int y; // 窗口起始Y坐标
    private String title = null;
    private String message = null;
    private JPanel mainPanel; // 主面板
    private JLabel titleLabel; // 标题栏标签
    private JPanel titlePanel; // 标题栏面板
    private JLabel messageLabel; // 内容标签
    private JPanel messagePanel; // 内容面板

    public MessageBox(String title,String message) {
        this.title = title;
        this.message = message;
        this.init();
        new Thread(this).start();
    }

    private void init() {
        //获取工具栏高度
        bottomToolKitHeight = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
        //获取屏幕尺寸
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

        screenWidth = dimension.width;
        screenHeight = dimension.height;

        //屏幕以左上角为原点
        x = screenWidth - windowWidth;
        y = screenHeight;

        this.setLocation(x, y - bottomToolKitHeight - windowHeight);
        mainPanel = new JPanel(new BorderLayout());

        titleLabel = new JLabel(title);
        titleLabel.setForeground(new Color(25,29,52));
        titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        /**
         * 设置标题背景色彩
         */
        titlePanel.setBackground(new Color(25,29,52));
        titlePanel.add(titleLabel);

        messageLabel = new JLabel(message);
        messagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        messagePanel.add(messageLabel);

        messageLabel.setForeground(Color.WHITE);
        titleLabel.setForeground(Color.WHITE);
        messageLabel.setFont(new Font("KaiTi",10,15));
        /**
         * 设置内容的背景颜色
         * */
        messagePanel.setBackground(new Color(25,39,82));

        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(messagePanel, BorderLayout.CENTER);

        this.setSize(windowWidth, windowHeight);
        this.setAlwaysOnTop(false);
        this.getContentPane().add(mainPanel);

        this.addMouseListener(this);

        Toolkit.getDefaultToolkit().beep(); // 播放系统声音，提示一下

        this.setVisible(true);
    }

    @Override
    public void run() {
        int delay = 7;
        int step = 1;
        int end = windowHeight + bottomToolKitHeight;
        java.io.File file = new java.io.File("pic/up.mp3");
        String url = file.toURI().toString();
        Media media = new Media(url);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
        while (true) {
            try {
                step++;
                y = y - 1;
                this.setLocation(x, y);
                if (step > end) {
                    Thread.sleep(stayTime);
                    break;
                }
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        step = 1;
        while (true) {
            try {
                step++;
                y = y + 1;
                this.setLocation(x, y);
                if (step > end) {
                    this.dispose();
                    break;
                }
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        this.dispose();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}
